package cn.itcast.reggie.service.impl;

import cn.itcast.reggie.common.BaseContext;
import cn.itcast.reggie.mapper.*;
import cn.itcast.reggie.pojo.*;
import cn.itcast.reggie.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    @Transactional
    public void submit(Orders orders) {
        //3.补齐订单中属性，保存订单对象
        String number = IdWorker.getIdStr();//通过雪花算法生成订单号
        Long userId = BaseContext.getCurrentId(); //获取登录用户id
        //根据地址id获取地址对象
        AddressBook addressBook = addressBookMapper.selectById(orders.getAddressBookId());
        //获取购物车数据
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
        //根据登录用户id获取用户对象
        User user = userMapper.selectById(userId);

        orders.setNumber(number);
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setAmount(sum(shoppingCarts));
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        orders.setUserName(user.getName());

        this.save(orders);


        //4.获取登录用户购物车数据，将每个购物项，将购物项中信息封装订单明细中保存订单明细包；
        for (ShoppingCart shoppingCart : shoppingCarts) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orders.getId());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetailMapper.insert(orderDetail);
        }
        //5.清空购物车数据
        shoppingCartMapper.delete(queryWrapper);
    }

    private BigDecimal sum(List<ShoppingCart> shoppingCarts) {
        BigDecimal bigDecimal = new BigDecimal(0);
        for (ShoppingCart shoppingCart : shoppingCarts) {
            Integer number = shoppingCart.getNumber();
            BigDecimal amount = shoppingCart.getAmount();
            bigDecimal = bigDecimal.add(new BigDecimal(number).multiply(amount));
        }
        return bigDecimal;
    }
}